package card;

/**
 * Class Deck is a cardset contains 54 cards
 * 
 * 
 * @version 2.1.3
 */
public class Deck extends Cardset {

	/**
	 * construct a cardset which consists of 54 cards with correct value and
	 * suit
	 */
	public Deck() {
		super(54);
		for (int i = 0; i < 54; i++) {
			Card temp = new Card();
			setAllSuit(i, temp);
			setAllValue(i, temp);
			super.add(temp);
		}
	}

	/**
	 * Set all suit of the cards
	 * 
	 * @param i
	 *            index of cards array
	 * @param temp
	 *            the cards array
	 */
	private void setAllValue(int i, Card temp) {
		if (i == 52)
			temp.setValue('B');
		else if (i == 53)
			temp.setValue('R');
		else if (i % 13 > 0 && i % 13 < 9)
			temp.setValue((char) ('2' - 1 + i % 13));
		else {
			switch (i % 13) {
			case 0:
				temp.setValue('A');
				break;
			case 9:
				temp.setValue('X');
				break;
			case 10:
				temp.setValue('J');
				break;
			case 11:
				temp.setValue('Q');
				break;
			case 12:
				temp.setValue('K');
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Set all values of the cards
	 * 
	 * @param i
	 *            index of cards array
	 * @param temp
	 *            the cards array
	 */
	private void setAllSuit(int i, Card temp) {
		switch (i / 13) {
		case 0:
			temp.setSuit('s');
			break;
		case 1:
			temp.setSuit('h');
			break;
		case 2:
			temp.setSuit('c');
			break;
		case 3:
			temp.setSuit('d');
			break;
		case 4:
			temp.setSuit('g');
			break;
		default:
			break;
		}
	}

	/**
	 * Shuffle the Deck so that the Card objects are randomly arranged.
	 */
	public void shuffle() {
		for (int i = 0; i < (53); i++) {
			int r = i + (int) Math.floor(Math.random() * (54 - i));
			Card temp = new Card(cards[i]);
			cards[i] = new Card(cards[r]);
			cards[r] = new Card(temp);
		}
	}
}
